<?php

class ProductController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        // action body
    }

    public function newAction() {
        $result = Application_Model_ProductMapper::getNewProducts(20);
        $this->view->products = $result;
    }

    public function viewAction() {
        $request = $this->getRequest();
        $id = $request->getParam("id");
        //
        $product = Application_Model_ProductMapper::fetch($id);
        $this->view->product = $product;
        //
        $auth = Zend_Auth::getInstance();
        $login = $auth->hasIdentity();
        $this->view->login = $login;
        //
        $replies = Application_Model_ReplyMapper::fetchByProductID($id);
        $this->view->replies = $replies;
    }

    public function replyAction() {
        $request = $this->getRequest();
        $productID = $request->getParam("id");
        $content = $request->getParam("content");
        $auth = Zend_Auth::getInstance();
        $email = $auth->getIdentity();
        $accountID = Application_Model_AccountMapper::getIDByEmail($email);
        $reply = new Application_Model_Reply($content, $accountID, $productID);
        Application_Model_ReplyMapper::insert($reply);
        $this->getResponse()->setRedirect("/product/view?id=" . $productID);
    }

    public function hotAction() {
        $result = Application_Model_ProductMapper::getHotProducts(20);
        $this->view->products = $result;
    }

    public function typeAction() {
        $request = $this->getRequest();
        $typeID = $request->getParam("id");
        $type = Application_Model_TypeMapper::fetch($typeID);
        $products = Application_Model_ProductMapper::fetchByType($typeID);
        $this->view->typeName = $type[C_NAME];
        $this->view->products = $products;
    }

}

